package com.leyou.controller;

import com.leyou.dto.PageDTO;
import com.leyou.dto.SpecParamDTO;
import com.leyou.dto.SpuDTO;
import com.leyou.dto.SpuDetailDTO;
import com.leyou.pojo.SpecParam;
import com.leyou.pojo.Spu;
import com.leyou.pojo.SpuDetail;
import com.leyou.service.SkuService;
import com.leyou.service.SpuDetailService;
import com.leyou.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jshstart
 * @creat 2020-12-11 19:11
 */
@RestController
@RequestMapping("/goods")
public class SpuController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SkuService skuService;
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "rows",required = false) Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
       PageDTO<SpuDTO> pageDTO= spuService.querySpuByPage(brandId,categoryId,id,page,rows,saleable);
       return ResponseEntity.ok(pageDTO);
    }
    @PostMapping
    public ResponseEntity<Void> addGood(@RequestBody SpuDTO spuDTO){
        spuService.addGood(spuDTO);
        return ResponseEntity.ok().build();
    }
    @PutMapping
    public ResponseEntity<Void> updateGood(@RequestBody SpuDTO spuDTO){
        spuService.updateGood(spuDTO);
        return ResponseEntity.ok().build();
    }
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailBySpuId(@RequestParam("id") Long id){
        SpuDetail spuDetail = spuDetailService.query().eq("spu_id", id).one();
        SpuDetailDTO spuDetailDTO = new SpuDetailDTO(spuDetail);
        return ResponseEntity.ok(spuDetailDTO);
    }
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> queryParamBySpuId(
            @RequestParam(value = "id",required = true) Long id,
            @RequestParam(value = "searching",required = false) Boolean searching){
        List<SpecParamDTO> list=spuDetailService.queryBySpuId(id,searching);
        return ResponseEntity.ok(list);
    }
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleableBySpuId(@RequestParam(value = "id",required = false) Long id,@RequestParam(value = "saleable",required = false) Boolean saleable){
        if (saleable!=null) {
            spuService.updateSaleableBySpuId(id, saleable);
        }
        return ResponseEntity.ok().build();
    }
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryAllBySpuId(@PathVariable("id") Long id){
     SpuDTO spuDTO= spuService.queryAllBySpuId(id);
      return ResponseEntity.ok(spuDTO);
    }
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        Spu spu = spuService.getById(id);
        return ResponseEntity.ok(new SpuDTO(spu));
    }
    @PutMapping("/stock/minus")
   public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        System.out.println("cartMap = " + cartMap);
        skuService.deductStock(cartMap);
        return ResponseEntity.ok().build();
    }
}
